using System;
using System.Collections;
using System.Collections.Generic;
using Newtonsoft.Json;
using UnityEngine;
using UnityEngine.UI;

public class Job721Panel : MonoBehaviour
{
    public Transform ToggleGroup, ShopItemGroup, MaxTipePanle;
    public Toggle toggle;
    public Job721Item shopItem, TepmItem;
    public Text toggleText, nameText, DesText, BuyNumText;
    int _index;
    List<Job712Data> Job712Data;
    List<Job721Item> shopItems = new List<Job721Item>();
    string[] strs = new string[] { "技能", "皮肤", "宠物", "脚印", "表情" };
    int num = 1;
    int toggle_Index;
    // Start is called before the first frame update
    void Start()
    {
        Job712Data = JsonConvert.DeserializeObject<List<Job712Data>>(Resources.Load<TextAsset>("Job712Data").text);
        shopItem.gameObject.SetActive(false);

        for (int i = 0; i < strs.Length; i++)
        {
            int index = i;

            toggleText.text = strs[i];

            var tog = Instantiate(toggle, ToggleGroup);

            tog.onValueChanged.AddListener((b) =>
            {
                if (b)
                {

                }
            });
        }
        toggle.gameObject.SetActive(false);

        ChangeList(Job712Data);
    }

    private void ChangeList(List<Job712Data> shopDatas)
    {
        for (int i = 0; i < shopDatas.Count; i++)
        {
            GetItem(shopDatas[i], i);
        }
    }


    private void GetItem(Job712Data shopData, int i)
    {
        if (shopItems.Count <= i)
        {
            shopItems.Add(Instantiate(shopItem, ShopItemGroup));
        }
        shopItems[i].gameObject.SetActive(true);

        shopItems[i].init(shopData, this);
    }
    public void SetItem(int id)
    {
        if (TepmItem != null)
        {
            TepmItem.SetBg(false);
        }
        TepmItem = shopItems[id];
        TepmItem.SetBg(true);
        MaxTipePanle.gameObject.SetActive(true);
        refreshItem(id);
    }
    public void LeftBut()
    {
        _index--;
        if (_index < 0)
        {
            _index = Job712Data.Count - 1;
        }
        SetItem(_index);
    }
    public void RightBut()
    {
        _index++;
        if (_index >= Job712Data.Count)
        {
            _index = 0;
        }
        SetItem(_index);
    }

    internal void refreshItem(int id)
    {
        MaxTipePanle.gameObject.SetActive(true);
        nameText.text = Job712Data[id].name;
        DesText.text = Job712Data[id].des;
        _index = id;
    }
    // Update is called once per fram
}
public class Job712Data
{
    public string name;
    public int id;
    public string des;
}